import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 自动生成的调用父类的代码删除
//        super.doGet(req, resp);

        String method = req.getMethod();
        System.out.println(method);

        // 这个代码是打印在服务器的控制台上
        System.out.println("hello world");
        // 这个打印是打印在响应报文上，显示到页面上
        // 就是把 hello world 字符串作为响应报文的 body 了, 浏览器就会把这个 body 显示在页面上
        resp.getWriter().write("hello world");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost");
        resp.getWriter().write("doPost");
    }

//    public class ExampleServlet extends HttpServlet {
//        @Override
//        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//            // 获取请求方法
//            String method = request.getMethod();
//
//            System.out.println(method);
//
//            // 设置响应内容类型
//            response.setContentType("text/html");
//            PrintWriter out = response.getWriter();
//
//            // 输出请求方法
//            out.println("<html><body>");
//            out.println("<h1>Request Method: " + method + "</h1>");
//            out.println("</body></html>");
//        }
//    }
}
